import {ADMIN_QUERY_USER} from '@/store/common/admin/type'
import {put, select, takeEvery} from "redux-saga/effects";
import Http from "@/util/http";
import {errorMsg, load} from "@/util/global";
import {InitUserInfo4Obj} from '@/store/common/admin/action';

const effects = {
    /*查询当前Page页的userList数据*/
    * QueryUserList() {
        yield load(true);
        let currentPage = yield select(state => state.toJS().admin.UserCurrentPage);
        let pageSize = yield select(state => state.toJS().admin.UserListPageSize);

        if (currentPage === undefined || pageSize === undefined ||
            parseInt(currentPage) <= 0 || parseInt(pageSize) <= '0') {
            errorMsg("数据错误，当前页数：" + currentPage + "，单页展示条数：" + pageSize + "。")
            yield load(false);
            return;
        }
        const ajax = yield Http.get(`/sys/admin/user/queryUserList?page=${currentPage}&size=${pageSize}`);
        if (ajax.success === false) {
            yield load(false);
            return;
        }

        const data = {
            UserList: ajax.data.rows,
            UserCurrentPage: ajax.data.page,
            UserTotalPage: Math.ceil(ajax.data.total / pageSize),
            UserCounts: ajax.data.total
        };
        yield put(InitUserInfo4Obj(data));
        yield load(false);
    },
    /*查询下一Page页的userList数据*/
    * QueryNextPageUserList() {

    },
    /*查询上一Page页的userList数据*/
    * QueryPrevPageUserList() {

    }
}
export default function* Admin() {
    yield takeEvery(ADMIN_QUERY_USER, effects.QueryUserList);
    // yield takeEvery(ADMIN_QUERY_NEXT_USER, effects.QueryNextPageUserList);
    // yield takeEvery(ADMIN_QUERY_Prev_USER, effects.QueryPrevPageUserList);
}
